//
//  NSMutableAttributedString+Extension.m
//  FamilyDoctorUser
//
//  Created by leqiang222 on 2017/6/19.
//  Copyright © 2017年 kinglian. All rights reserved.
//

#import "NSMutableAttributedString+Extension.h"

@implementation NSMutableAttributedString (Extension)

+ (NSMutableAttributedString *)app_setUnderlineStyleWithString:(NSString *)string color:(UIColor *)color {
    NSMutableAttributedString *attSringM = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange strRange = {0, [string length]};
    [attSringM addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [attSringM addAttribute:NSForegroundColorAttributeName value:color range:strRange];
    
    return attSringM;
}

+ (NSMutableAttributedString *)app_setAttributedColor:(UIColor *)color totalStr:(NSString *)totalStr colorStr:(NSString *)colorStr {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    
    NSArray *rangeArray = [self rangesOfTotalStr:totalStr attachStr:colorStr];
    
    for (int i = 0; i < rangeArray.count; i++) {
        NSRange range =  NSRangeFromString(rangeArray[i]);
        [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
    
    return attributedStr;
}

+ (NSArray *)rangesOfTotalStr:(NSString *)totalStr attachStr:(NSString *)attachStr {
    NSMutableArray *results = [NSMutableArray array];
    
    NSRange searchRange = NSMakeRange(0, [totalStr length]);
    NSRange range;
    
    while ((range = [totalStr rangeOfString:attachStr options:0 range:searchRange]).location != NSNotFound) {
        [results addObject:NSStringFromRange(range)];
        
        searchRange = NSMakeRange(NSMaxRange(range), [totalStr length] - NSMaxRange(range));
    }
    
    return results;
}

@end
